package com.jeomo.masterdata.starter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jeomo.base.starter.service.impl.BaseServiceImpl;
import com.jeomo.masterdata.starter.entity.Goods;
import com.jeomo.masterdata.starter.entity.GoodsType;
import com.jeomo.masterdata.starter.mapper.GoodsMapper;
import com.jeomo.masterdata.starter.mapper.GoodsTypeMapper;
import com.jeomo.masterdata.starter.service.IGoodsTypeService;
import com.jeomo.masterdata.starter.util.CommonUtil;
import com.jeomo.shiro.dto.LoginInfo;
import com.jeomo.shiro.util.LoginSessionUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: qbt
 * @Date: 2020/5/8 22:18
 * @Version 1.0
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class GoodsTypeServiceImpl extends BaseServiceImpl<GoodsTypeMapper, GoodsType> implements IGoodsTypeService {

    @Resource
    private GoodsTypeMapper goodsTypeMapper;

    @Resource
    private GoodsMapper goodsMapper;

    @Override
    public int create(GoodsType goodsType) {
        LoginInfo currentLoginInfo = LoginSessionUtil.getCurrentLoginInfo();
        String currentOrgCode = currentLoginInfo.getCurrentOrgCode();
        goodsType.setOrgCode(currentOrgCode);
        goodsType.setTopOrgCode(currentLoginInfo.getCurrentTopOrgCode());
        String recentlyCode = goodsTypeMapper.queryRecentlyCode(currentOrgCode);
        String code = CommonUtil.spliceCode(currentOrgCode, recentlyCode);
        goodsType.setCode(code);
        return goodsTypeMapper.insert(goodsType);
    }

    @Override
    public List<Goods> removeGoodsTypeNodes(String goodsTypeCode) {
        List<Goods> goodsList = new ArrayList<>();
        ArrayList<String> goodsTypeCodes = new ArrayList<>();
        ArrayList<String> lastChildren = new ArrayList<>();
        //先把要删除的一级分类code放入到集合中
        goodsTypeCodes.add(goodsTypeCode);
        //递归的将一级分类下的code也加入到集合中
        this.getIds(goodsTypeCodes, goodsTypeCode, lastChildren);
        for (String lastNode : lastChildren) {
            QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("goods_type_code", lastNode);
            List<Goods> goods = goodsMapper.selectList(queryWrapper);
            goodsList.addAll(goods);
        }
        //如果查出的商品列表为空，证明lastChildren没有商品引用，可以删除。
        if (goodsList.isEmpty()) {
            goodsTypeMapper.deleteBatchIds(goodsTypeCodes);
        }
        return goodsList;
    }

    private void getIds(ArrayList<String> goodsTypeCodes, String code, ArrayList<String> lastChildren) {
        //查询二级分类的对象
        QueryWrapper<GoodsType> Wrapper = new QueryWrapper<>();
        Wrapper.eq("parent_code", code);
        List<GoodsType> goodsTypes = goodsTypeMapper.selectList(Wrapper);
        //如果goodsTypes为空证明他是最后一个节点没有子节点了。
        if (goodsTypes.isEmpty()) {
            lastChildren.add(code);
        }
        //遍历二级分类的对象，把二级分类的id加入到要删除的集合中
        for (GoodsType goodsType : goodsTypes) {
            String goodsTypeCode = goodsType.getCode();
            goodsTypeCodes.add(goodsTypeCode);
            //把二级分类的每一个ID，查询它下面的子节点
            this.getIds(goodsTypeCodes, goodsTypeCode, lastChildren);
        }
    }

}
